namespace OpenWaves.HtmlEncoding
{
    public class WordWrappingHtmlWriterDecorator : IHtmlWriter
    {
        private readonly IHtmlWriter inner;
        private int currentContentLength;

        public WordWrappingHtmlWriterDecorator(IHtmlWriter inner)
        {
            this.inner = inner;
        }

        public void WriteText(string text)
        {
            for (int i = 0; i < text.Length; i++)
            {
                if (currentContentLength == 20)
                {
                    WriteTag("&shy;");
                }
                if (char.IsWhiteSpace(text[i]))
                {
                    currentContentLength = 0;
                }
                else
                {
                    currentContentLength++;
                }
                inner.WriteText(text[i].ToString());
            }
        }

        public void WriteTag(string tag)
        {
            currentContentLength = 0;
            inner.WriteTag(tag);
        }

        public override string ToString()
        {
            return inner.ToString();
        }
    }
}